<?php

namespace addons\shop\controller;

use addons\shop\model\Block;
use addons\shop\model\Category;
use addons\shop\model\Goods;
use addons\shop\model\Menu;
use think\Config;
use think\Db;

/**
 * 商城首页控制器
 * Class Index
 * @package addons\shop\controller
 */
class Index extends Base
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $config = get_addon_config('shop');

        //设置TKD
        Config::set('shop.title', $config['title'] ?: '首页');
        Config::set('shop.keywords', $config['keywords']);
        Config::set('shop.description', $config['description']);

        $this->view->assign('indexCategoryList', Category::getIndexCategoryList());
        $this->view->assign('indexGoodsList', Goods::getIndexGoodsList());
        $this->view->assign('indexFocusList', Block::getBlockListByName('indexfocus'));
        $train_one     = Db::name("inquiry_problem i")
            ->field("i.*,u.username,u.avatar,u.nickname")
            ->join("user u", 'u.id=i.uid')
            ->where('delete_time','null')
            ->limit(6)
            ->order("i.id desc")
            ->select();

        $lb_train_one = Db::name("inquiry_problem")->where(['delete_time' => null])->limit(3)->select();
        $lb_train_one = array_map(function ($item) {
            $images       = $item['imgs'];
            $images       = explode(',', $images);
            $images       = array_map(function ($item) {
                return cdnurl($item);
            }, $images);
            $item['imgs'] = $images[0] ?? '';
            return $item;
        }, (array)$lb_train_one);

        $shop_category = Db::name("shop_category")->limit(10)->select();
        $shop_goods    = Db::name("shop_goods")->where(['deletetime' => null])->limit(20)->select();
        $train_article = Db::name("train_article")->where(['type' => 1])->where(['delete_time' => null])->limit(8)->select();
        $trainjy       = Db::name("train_article")->where(['type' => 2])->where(['delete_time' => null])->limit(3)->select();
        $this->view->assign('datajy', $trainjy);
        $this->view->assign('data', $train_one);
        $this->view->assign('datalb', $lb_train_one);
        $this->view->assign('shop_category', $shop_category);
        $this->view->assign('shop_goods', $shop_goods);
        $this->view->assign('train_article', $train_article);
        $this->view->assign('t_type', 1);

        return $this->view->fetch('/index');
    }

}
